﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Food_Entity : EntityBase<FOOD>
    {
        public override RESP_Info Add(FOOD model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into FOOD(FOOD_ID,BREAKFAST,LUNCH,DINNER) values (@FOOD_ID,@BREAKFAST,@LUNCH,@DINNER)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("FOOD_ID", model.FOOD_ID));
                    cmd.Parameters.Add(new SqlParameter("BREAKFAST", model.BREAKFAST));
                    cmd.Parameters.Add(new SqlParameter("LUNCH", model.LUNCH));
                    cmd.Parameters.Add(new SqlParameter("DINNER", model.DINNER));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加餐饮成功</p></h5>";
                    info.Url = "FoodList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加餐饮失败</p></h5>";
                    info.Url = "FoodAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加餐饮失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(FOOD model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update FOOD set BREAKFAST=@BREAKFAST,LUNCH=@LUNCH,DINNER=@DINNER where FOOD_ID=@FOOD_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("FOOD_ID", model.FOOD_ID));
                    cmd.Parameters.Add(new SqlParameter("BREAKFAST", model.BREAKFAST));
                    cmd.Parameters.Add(new SqlParameter("LUNCH", model.LUNCH));
                    cmd.Parameters.Add(new SqlParameter("DINNER", model.DINNER));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑餐饮成功</p></h5>";
                    info.Url = "FoodList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑餐饮失败</p></h5>";
                    info.Url = "FoodAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑餐饮失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(FOOD food)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from FOOD where FOOD_ID=@FOOD_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("FOOD_ID", food.FOOD_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除餐饮成功</p></h5>";
                    info.Url = "FoodList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除餐饮失败</p></h5>";
                    info.Url = "FoodList.aspx";

                    Tom.Net.Log.LogManager.Error("删除餐饮失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override FOOD GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select FOOD_ID,BREAKFAST,LUNCH,DINNER from FOOD where FOOD_ID=@FOOD_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("FOOD_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        FOOD food = new FOOD();
                        if (!reader.IsDBNull(0)) food.FOOD_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) food.BREAKFAST = reader.GetString(1);
                        if (!reader.IsDBNull(2)) food.LUNCH = reader.GetString(2);
                        if (!reader.IsDBNull(3)) food.DINNER = reader.GetString(3);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return food;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取餐饮数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override FOOD GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        FOOD food = new FOOD();
                        if (!reader.IsDBNull(0)) food.FOOD_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) food.BREAKFAST = reader.GetString(1);
                        if (!reader.IsDBNull(2)) food.LUNCH = reader.GetString(2);
                        if (!reader.IsDBNull(3)) food.DINNER = reader.GetString(3);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return food;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取餐饮数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<FOOD> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<FOOD> foods = new List<FOOD>();
                    while (reader.Read())
                    {
                        FOOD food = new FOOD();
                        if (!reader.IsDBNull(0)) food.FOOD_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) food.BREAKFAST = reader.GetString(1);
                        if (!reader.IsDBNull(2)) food.LUNCH = reader.GetString(2);
                        if (!reader.IsDBNull(3)) food.DINNER = reader.GetString(3);

                        foods.Add(food);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return foods;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取餐饮数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
